import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/other/resource_entity.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:ball_lion/module/pick/items/banner_scaffold.dart';
import 'package:ball_lion/widgets/card.dart';
import 'package:ball_lion/widgets/loading/loading_widget.dart';

import 'package:ball_lion/server/api/other_api.dart';
import 'ai_type.dart';

class AiExamplePage extends StatefulWidget {
  const AiExamplePage({super.key});

  @override
  State<AiExamplePage> createState() => _AiExamplePageState();
}

class _AiExamplePageState extends State<AiExamplePage> {
  List<ResourceEntity>? example;
  AiType? type = Get.arguments is AiType
      ? Get.arguments
      : AiType.getTypeByTag(Get.arguments);

  Future getData() async {
    example = [];
    var list = await OtherApi.getAppList("app_ai_example");
    list?.forEach((element) {
      if (element.button == (type?.label ?? "")) {
        example?.add(element);
      }
    });
    update();
  }

  @override
  void initState() {
    getData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BannerScaffold(
        type: "${type?.label ?? ""}_example",
        title: type?.name ?? "",
        body: (physics) => LoadingWidget<bool>(
              isLoading: example == null,
              data: example?.length == 0,
              child: () => Column(
                children: [
                  Block(
                      cardStyle: const CardStyle(
                          margin: EdgeInsets.symmetric(
                              horizontal: 8, vertical: 12)),
                      child: CachedNetworkImage(
                          imageUrl: example!.first.imgUrl ?? ""))
                ],
              ),
            ));
  }
}
